package org.ims.inv.service.flow.impl;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.ims.inv.dao.flow.DAOFlowDefinition;
import org.ims.inv.model.flow.ModelFlowDef;
import org.ims.inv.service.flow.ServiceFlowDefinition;

import cn.trymore.core.exception.DAOException;
import cn.trymore.core.exception.ServiceException;
import cn.trymore.core.service.impl.ServiceGenericImpl;

/**
 * Service: 工作流定义
 * 
 * @author Jeccy.Zhao
 *
 */
public class ServiceFlowDefinitionImpl
extends ServiceGenericImpl<ModelFlowDef> implements ServiceFlowDefinition
{
	
	/**
	 * Repository of flow form
	 */
	private DAOFlowDefinition daoFlowDefinition;
	
	public ServiceFlowDefinitionImpl(DAOFlowDefinition dao)
	{
		super(dao);
		
		this.setDaoFlowDefinition(dao);
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.ims.inv.service.flow.ServiceFlowDefinition#getCurrentAppliedDefinition()
	 */
	public ModelFlowDef getCurrentAppliedDefinition () throws ServiceException
	{
		try
		{
			return (ModelFlowDef)this.daoFlowDefinition.findUnique(
					"from ModelFlowDef d where d.defApplied = " + ModelFlowDef.DEF_APPLIED, null);
		} 
		catch (DAOException e)
		{
			throw new ServiceException(e);
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see cn.trymore.core.service.impl.ServiceGenericImpl#getCriterias(cn.trymore.core.model.ModelBase)
	 */
	@Override
	protected DetachedCriteria getCriterias(ModelFlowDef entity)
	{
		DetachedCriteria criteria = DetachedCriteria.forClass(ModelFlowDef.class);
		criteria.addOrder(Order.desc("defCreationTime"));
		return criteria;
	}
	
	public void setDaoFlowDefinition(DAOFlowDefinition daoFlowDefinition)
	{
		this.daoFlowDefinition = daoFlowDefinition;
	}

	public DAOFlowDefinition getDaoFlowDefinition()
	{
		return daoFlowDefinition;
	}

}
